<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="80px"
    >
      <el-form-item label="请求时间">
        <el-date-picker
          v-model="daterangeRequestDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="请求路径" prop="requestUrl">
        <el-input
          v-model="queryParams.requestUrl"
          placeholder="请输入请求路径"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['integrate:integrateLog:remove']"
          >删除</el-button
        >
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['integrate:integrateLog:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      border
      :data="integrateLogList"
      max-height="580"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="接口编码" align="center" prop="inteCode" />
      <el-table-column
        label="请求时间"
        align="center"
        prop="requestDate"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.requestDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="请求路径" align="center" prop="requestUrl" />
      <el-table-column label="请求参数" align="center" prop="requestBody" />
      <el-table-column label="接口状态" align="center" prop="requestStatus" />
      <el-table-column label="接口返回" show-overflow-tooltip width="240" align="center" prop="responseResult" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="View"
            @click="handleView(scope.row, scope.index)"
            v-hasPermi="['integrate:integrateLog:query']"
            >详细</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改接口日志对话框 -->
    <el-dialog title="操作日志详细" v-model="open" :close-on-click-modal="false" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" style='max-height:650px;overflow-y: auto;'>
        <el-form-item label="请求时间" prop="requestDate">
          {{ form.requestDate }}
        </el-form-item>
        <el-form-item label="请求路径" prop="requestUrl">
          {{ form.requestUrl }}
        </el-form-item>
        <el-form-item label="请求参数" prop="requestBody">
          <span style='width:100%'>{{ form.requestBody }}</span>
        </el-form-item>
        <el-form-item label="接口返回" prop="responseResult">
          <span style='width:100%'>{{ form.responseResult }}</span>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          {{ form.remark }}
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {
  listIntegrateLog,
  getIntegrateLog,
  updateIntegrateLog,
  addIntegrateLog,
  delIntegrateLog,
} from "@/api/system/integrateLog";

export default {
  name: "IntegrateLog",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 接口日志表格数据
      integrateLogList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 备注时间范围
      daterangeRequestDate: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        inteCode: null,
        requestDate: null,
        requestUrl: null,
        requestStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        inteCode: [
          { required: true, message: "接口编码不能为空", trigger: "change" },
        ],
        requestDate: [
          { required: true, message: "请求时间不能为空", trigger: "blur" },
        ],
        requestUrl: [
          { required: true, message: "请求路径不能为空", trigger: "blur" },
        ],
        requestBody: [
          { required: true, message: "请求参数不能为空", trigger: "blur" },
        ],
        requestStatus: [
          { required: true, message: "接口状态不能为空", trigger: "blur" },
        ],
        responseResult: [
          { required: true, message: "接口返回不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询接口日志列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (
        null != this.daterangeRequestDate &&
        "" != this.daterangeRequestDate
      ) {
        this.queryParams.params["beginRequestDate"] =
          this.daterangeRequestDate[0];
        this.queryParams.params["endRequestDate"] =
          this.daterangeRequestDate[1];
      }
      listIntegrateLog(this.queryParams).then((response) => {
        this.integrateLogList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        inteCode: null,
        requestDate: null,
        requestUrl: null,
        requestBody: null,
        requestStatus: "0",
        responseResult: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeRequestDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 修改按钮操作 */
    handleView(row) {
      this.reset();
      const id = row.id || this.ids;
      getIntegrateLog(id).then((response) => {
        this.form = response.data;
        this.open = true;
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除接口日志编号为"' + ids + '"的数据项？')
        .then(function () {
          return delIntegrateLog(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "integrate/integrateLog/export",
        {
          ...this.queryParams,
        },
        `integrateLog_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
